package lc01;

import java.util.Hashtable;
import java.util.Map;

/**
 * @Author Stringzhua
 * @Date 2024/3/26 17:33
 * description:两数之和
 * 哈希表求解
 * key value
 */
public class Test02 {
    public int[] twoSum(int[] nums, int target) {
        System.out.println("aaa");
        int[] result = new int[2];//创建数组,长度为2
        Map map = new Hashtable();
        for (int i = 0; i < nums.length; i++) {
            map.put(nums[i], i);//将数组存入hashtable
        }
        for (int j = 0; j < nums.length; j++) {
            int diff = target - nums[j];
//            if (map.containsKey(diff) && map.get(diff) != j)
            if (map.containsKey(diff) && !((Integer) map.get(diff)).equals(j)) {
                result[0] = j;
                result[1] = (int) map.get(diff);
                return result;
            }
        }
        return result;
    }


    public static void main(String[] args) {
        int nums[] = {3, 3};
        int target = 6;
        Test02 test01 = new Test02();
        int[] ints = test01.twoSum(nums, target);
        for (int i = 0; i < ints.length; i++) {
            System.out.print(i + " ");
        }
    }
}
